package com.hua.fanqiemall.product.service.impl;

import com.hua.fanqiemall.product.entity.AttrEntity;
import com.hua.fanqiemall.product.service.AttrService;
import com.hua.fanqiemall.product.vo.AttrGroupWithAttrsRspVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hua.common.utils.PageUtils;
import com.hua.common.utils.Query;

import com.hua.fanqiemall.product.dao.AttrGroupDao;
import com.hua.fanqiemall.product.entity.AttrGroupEntity;
import com.hua.fanqiemall.product.service.AttrGroupService;

/**
 * 属性分组
 * @author luoweihua
 * @date 2022/1/4 23:18
 */
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    /**
     * 元素分页查询
     * @param params 查询参数
     * @return PageUtils
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 分页查询属性分组信息
     * @author luoweihua
     * @date 2022/1/12 15:40
     * @param params 查询参数
     * @param catalogId 分类ID
     * @return PageUtils
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catalogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(key)){
            wrapper.and((obj)-> obj.eq("attr_group_id",key).or().like("attr_group_name",key));
        }
        if (catalogId == 0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
            return new PageUtils(page);
        }else {
            wrapper.eq("catelog_id",catalogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    /**
     * 获取分类下所有分组&关联属性接口
     * @author luoweihua
     * @date 2022/1/12 15:38
     * @param catelogId 分类ID
     * @return List<AttrGroupWithAttrsRspVo>
     */
    @Override
    public List<AttrGroupWithAttrsRspVo> getAttrGroupWithAttrs(Long catelogId) {
        List<AttrGroupWithAttrsRspVo> rspVoList = new ArrayList<>();
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        for (AttrGroupEntity attrGroupEntity : attrGroupEntities) {
            AttrGroupWithAttrsRspVo rspVo = new AttrGroupWithAttrsRspVo();
            BeanUtils.copyProperties(attrGroupEntity , rspVo);
            List<AttrEntity> relationAttr = attrService.getRelationAttr(attrGroupEntity.getAttrGroupId());
            if (relationAttr != null && relationAttr.size()>0) {
                rspVo.setAttrs(relationAttr);
            }
            rspVoList.add(rspVo);
        }
        return rspVoList;
    }

}